package com.oracle.lingshangejia.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.oracle.lingshangejia.Constant;
import com.oracle.lingshangejia.R;
import com.oracle.lingshangejia.bean.FieledMissionDetailsBean;

import java.util.List;

public class FieledMissionDetailsAdapter extends RecyclerView.Adapter<FieledMissionDetailsAdapter.ViewHolder> {

    private Context context;
    private OnItemClickListener listener;
    private List<FieledMissionDetailsBean.DataBean.WyjlBean> data;
    private String mGsfUrl;//社区医生头像
    private String mUfUrl;//居民头像
    public interface OnItemClickListener {
        void onItemClick(View view, int whyId);
    }
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
    public FieledMissionDetailsAdapter(Context context, List<FieledMissionDetailsBean.DataBean.WyjlBean> data,String usfUrl,String gsfUrl){
        this.data=data;
        this.context = context;
        mGsfUrl = usfUrl;
        mUfUrl = gsfUrl;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        View view = LayoutInflater.from(context).inflate(R.layout.item_fieledmissionsdetails,null);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        if(data.get(i).getWdType()==1){  //左侧显示
            viewHolder.ll_doctor_left.setVisibility(View.VISIBLE);
            viewHolder.ll_doctor_right.setVisibility(View.GONE);
            viewHolder.tv_left_txt.setText(data.get(i).getWdnr());
            Glide.with(context)
               .load(Constant.imgurl+mUfUrl)
                .apply(new RequestOptions().circleCrop().placeholder(R.drawable.ic_info_user)
                .error(R.drawable.ic_info_user))
                .into(viewHolder.iv_head_left);
        }else{  //右侧显示
            viewHolder.ll_doctor_left.setVisibility(View.GONE);
            viewHolder.ll_doctor_right.setVisibility(View.VISIBLE);
            viewHolder.tv_right_txt.setText(data.get(i).getWdnr());
            Glide.with(context)
                .load(Constant.imgurl+mGsfUrl)
                .apply(new RequestOptions().circleCrop().placeholder(R.drawable.ic_info_user)
                    .error(R.drawable.ic_info_user))
                .into(viewHolder.iv_head_right);

        }
        if(data.get(i).getImgUrl1().equals("")||TextUtils.isEmpty(data.get(i).getImgUrl1())){
            if (data.get(i).getWdType()==1) {
                viewHolder.ll_doctor_img.setVisibility(View.GONE);
            }else{
                viewHolder.ll_doctor_rightimg.setVisibility(View.GONE);
            }

        }else{
            if(data.get(i).getWdType()==1){
                viewHolder.ll_doctor_img.setVisibility(View.VISIBLE);
                RequestOptions options = new RequestOptions()
                        .placeholder(R.mipmap.ic_image_empty)
                        .error(R.mipmap.ic_image_empty)
                        .fallback(R.mipmap.ic_image_empty);
                Glide.with(context)
                        .load(Constant.imgurl+data.get(i).getImgUrl1())
                        .apply(options)
                        .into(viewHolder.iv_left_img);
                Glide.with(context)
                    .load(Constant.imgurl+mUfUrl)
                    .apply(new RequestOptions().circleCrop().placeholder(R.drawable.ic_info_user)
                        .error(R.drawable.ic_info_user))
                    .into(viewHolder.iv_left_head);
            }else{
                viewHolder.ll_doctor_rightimg.setVisibility(View.VISIBLE);
                RequestOptions options = new RequestOptions()
                        .placeholder(R.mipmap.ic_image_empty)
                        .error(R.mipmap.ic_image_empty)
                        .fallback(R.mipmap.ic_image_empty);
                Glide.with(context)
                        .load(Constant.imgurl+data.get(i).getImgUrl1())
                        .apply(options)
                        .into(viewHolder.iv_right_img);
                Glide.with(context)
                    .load(Constant.imgurl+mGsfUrl)
                    .apply(new RequestOptions().circleCrop().placeholder(R.drawable.ic_info_user)
                        .error(R.drawable.ic_info_user))
                    .into(viewHolder.iv_right_head);
            }

        }
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onItemClick(v,i);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return data!=null?data.size():0;
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        public LinearLayout ll_doctor_left;//左侧整个布局
        public LinearLayout ll_doctor_right;//右侧布局
        public LinearLayout ll_doctof_left_txt; //左边文字布局
        public ImageView iv_head_left;  //左边文字布局头像
        public TextView tv_left_txt; //z左边文字内容
        public ImageView iv_left_head;
        public ImageView iv_left_img;  //左边图片
        public LinearLayout ll_right_txt; //右侧文字布局
        public TextView tv_right_txt; //右侧文字
        public ImageView iv_head_right;//右侧文字头像
        public LinearLayout ll_doctor_rightimg;//图片条目
        public ImageView iv_right_img;  //右侧图片
        public ImageView iv_right_head;  //右侧头像
        public LinearLayout ll_doctor_img;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ll_doctof_left_txt=itemView.findViewById(R.id.ll_doctof_left_txt);
            iv_head_left=itemView.findViewById(R.id.iv_head_left);
            tv_left_txt=itemView.findViewById(R.id.tv_left_txt);
            iv_left_head=itemView.findViewById(R.id.iv_left_head);
            iv_left_img=itemView.findViewById(R.id.iv_left_img);
            ll_right_txt=itemView.findViewById(R.id.ll_right_txt);
            tv_right_txt=itemView.findViewById(R.id.tv_right_txt);
            iv_head_right=itemView.findViewById(R.id.iv_head_right);
            ll_doctor_rightimg=itemView.findViewById(R.id.ll_doctor_rightimg);
            iv_right_img=itemView.findViewById(R.id.iv_right_img);
            iv_right_head=itemView.findViewById(R.id.iv_right_head);
            ll_doctor_left=itemView.findViewById(R.id.ll_doctor_left);
            ll_doctor_right=itemView.findViewById(R.id.ll_doctor_right);
            ll_doctor_img=itemView.findViewById(R.id.ll_doctor_img);
        }
    }
}
